$(document).ready(function(){
	$.getJSON('controller/cont.portal.php',{logged:true},function(data){
			$('#nama').html(data.nama+' ('+data.gol+')');
			$('#unit').html(getString(data.jabatan)[0]);
	});

    getKewenangan();

    $.getJSON('controller/cont.login.php',{aksi_default:true},function(data){
        if(data){
            ubahPass();
            $("#passModal").click();
            $('#ket').html("Password Masih Default, Silahkan Ubah Password Anda");
        }
    });

    $.getJSON('controller/cont.login.php',{aksi_isAdmin:true},function(data){
        if(data.msg){
            getAllKanwil();
            $('#div_kanwil').css({
                'display' : 'block',
            });
        }else{
            getKanwil()
        }
    });

    // $.getJSON('controller/cont.login.php',{aksi_Kewenangan:true},function(data){
    //     if(data){

    //         document.getElementById("ijin_luar").style.display='block';
    //     }else{
    //         document.getElementById("ijin_luar").style.display="none";
    //     }
    // });

    // $.getJSON('controller/cont.login.php',{aksi_menu:true},function(data){
    //     if(data){
    //         document.getElementById("pic").style.display='block';
    //     }else{
    //         document.getElementById("pic").style.display="none";
    //     }
    // });

      
    $('#ecuti').on('click',function(e){
        window.location = 'ecuti.php';
    });
    $('#ijin_luar').on('click',function(e){
        window.location = 'main.php';
    });
	$('#simpan_password').click(function() {
        var id_user = $('#id_user').val();
        var password_lama = $('#password_lama').val();
        var password = $('#password_baru').val();
        var password2 = $('#password_baru2').val();
        
        if (password_lama === '' || password === '' || password2 === '') {
            $('#error').html('Semua Harus Diisi');
            var $toast = $( '#toast-example1').clone();
      		$('#alerts-container').append($toast.addClass('in'));
            clear_input();
        } else if (password !== password2) {
            $('#error').html('Konfirmasi Password Salah');
            var $toast = $( '#toast-example1').clone();
      		$('#alerts-container').append($toast.addClass('in'));
            clear_input();
        } else if (password.length < 6) {
            $('#error').html("Password Minimal 6 karakter");
            var $toast = $( '#toast-example1').clone();
            $('#alerts-container').append($toast.addClass('in'));
            $('.alert').show();
        } else {
            $.post('controller/cont.ruh.php', {aksi_ubah_password: true, id_user: id_user,
                password_lama: password_lama, password: password}, function(data) {
                if (data.msg) {
                    alert('Password Berhasil Diubah, Silahkan Logout dan Login Lagi');
                    clear_input();
                } else {
                    $('#error').html(data.error);
                    var $toast = $( '#toast-example1').clone();
                    $('#alerts-container').append($toast.addClass('in'));
                    clear_input();
                }
            }, 'json');
        }


        return false;
    });

	return false;
});

function ubahPass(){
	$.getJSON('controller/cont.ruh.php', {aksi_get_user_ubah_pass: true}, function(data) {
        document.getElementById('nip').value = data.nip;
        document.getElementById('id_user').value = data.id_user;
    });
}

function getProfil(){
	$('#loader').fadeIn();
	$('#profil').hide();
	$.getJSON('controller/cont.profil.php',{aksi_get_profil:true},function(data){
		document.getElementById('nip_td').innerHTML=data.pegawai.nip;
        document.getElementById('nama_td').innerHTML=data.pegawai.nama;
        document.getElementById('gol_td').innerHTML=data.golongan.nmgol+' ('+data.golongan.pangkat+')';
        document.getElementById('jabatan_td').innerHTML=getString(data.jabat)[0];
        document.getElementById('unit_td').innerHTML=getString(data.jabat)[1];
        document.getElementById('esel_td').innerHTML=getString(data.jabat)[2];
        $('#loader').fadeOut();
        $('#profil').show();
	});
}

function clear_input() {
    document.getElementById('password_lama').value = "";
    document.getElementById('password_baru').value = "";
    document.getElementById('password_baru2').value = "";
}

function getString(str){
	var jabat=str.split("\n");
	return jabat;
}

function getKewenangan(){
    $.getJSON('controller/cont.ruh.php',{aksi_get_all_kewenangan:true},function(data){
        $.each(data, function(index,data){
            var option2 = $('<option />');
            option2.attr('value', data.id_kewenangan).text(data.nama_kewenangan); 
            $('#kewenangan').append(option2);
        });
    });
}

function getAllKanwil(){
    $.getJSON('controller/cont.kanwil.php',{aksi_get_all_kanwil:true},function(data){
        $.each(data, function(index,data){
            var option = $('<option />');
            option.attr('value', data.kdkanwil).text(data.nmkanwil); 
            $('#kanwil').append(option);
        });
    });
}

function getKanwil(){
    $.getJSON('controller/cont.kanwil.php',{aksi_get_kanwil:true},function(data){
            var option = $('<option />');
            option.attr('value', data.kdkanwil).text(data.nmkanwil); 
            $('#kanwil').append(option);
    });
}